package com.hub.esdemo.service.msg;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson2.JSONObject;
import com.hub.esdemo.config.RabbitMQConfig;
import com.hub.esdemo.dto.Msg;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ author 飞天蝙蝠
 * @ date & time 2024/12/28 12:43
 */
@Service
public class RabbitMQSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Msg msg) {
        String uuid = UUID.fastUUID().toString();
        msg.setUuid(uuid);
        msg.setDataTime(DateUtil.now());
        System.out.println("Sending message: " + JSONObject.toJSONString(msg));
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, JSONObject.toJSONString(msg));
    }
}
